package com.suoyue.store.controller;

import com.suoyue.store.entity.CartObject;
import com.suoyue.store.entity.User;
import com.suoyue.store.service.IUserservice;
import com.suoyue.store.service.iCartService;
import com.suoyue.store.service.iLogService;
import com.suoyue.store.service.iOrderService;
import com.suoyue.store.service.impl.CartServiceImpl;
import com.suoyue.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController extends BaseController{
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private iCartService cartService;
    @Autowired
    private iLogService logService;
    @Autowired
    private iOrderService orderService;
    @Autowired
    private IUserservice userservice;
    @GetMapping({"get"})
    public JsonResult<List<CartObject>> getCartObjectsByUid(HttpSession session) {
        // 从Session中获取uid
        Integer uid = getUidFromSession(session);
        // 调用业务对象执行查询数据
        List<CartObject> data = cartService.getCart(uid);
        // 返回成功与数据
        return new JsonResult<List<CartObject>>(OK, data);
    }

    @RequestMapping("add")
    public JsonResult<String> addCartByCid(Integer cid){
        cartService.addCartByCid(cid);
        return new JsonResult<String>(OK);
    }
    @RequestMapping("minus")
    public JsonResult<String> minusCartByCid(Integer cid){
        cartService.minusCartByCid(cid);
        return new JsonResult<String>(OK);
    }
    @RequestMapping("delete")
    public JsonResult<String> deleteCartByCid(Integer cid){
        cartService.deleteCartByCid(cid);
        return new JsonResult<String>(OK);
    }

    @RequestMapping("insert")
    public JsonResult<String> insertCartByPid(HttpSession session,Integer pid,Integer num){
        cartService.insertCart(getUidFromSession(session),pid,num);
        return new JsonResult<String>(OK);
    }

    @RequestMapping("check")
    public JsonResult<String> check(HttpSession session){
        Integer uid=getUidFromSession(session);
        List<CartObject> cartList=cartService.getCart(uid);
        User user=userservice.findUserByUid(uid);

        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("网站购物信息提示");
        // 设置邮件发送者，与application.yml中设置一致
        message.setFrom("445484430@qq.com");
        // 设置邮件接收者
        message.setTo(user.getEmail());
        // 设置邮件发送日期
        message.setSentDate(new Date());
        StringBuffer email_text=new StringBuffer("xx电商网站\n尊敬的用户："+user.getUsername()+",您好\n您在本商城有一项购物记录，请确认\n");

        for(int i=0;i<cartList.size();i++){
            CartObject obj=cartList.get(i);
            logService.insertBuyLog(uid, obj.getPid());
            orderService.insert(uid,obj.getPid(), obj.getNum(), obj.getPrice()*obj.getNum());
            email_text.append((i+1)+"、 商品名："+obj.getName()+" 件数："+obj.getNum()+" 总价格："+ obj.getNum()* obj.getPrice()+"\n");
        }
        cartService.deleteAllCartByUid(uid);

        // 设置邮件的正文
        message.setText(email_text.toString());
        // 发送邮件
        javaMailSender.send(message);

        return new JsonResult<String>(OK);
    }
}
